#include <stdio.h>
#include <stdlib.h>
#define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0]))

void qsort(int array[], int begin, int end)
{
    if(begin >= end)
        return;

    int tmp = array[begin];
    int preIndex = begin, endIndex = end;

    while(endIndex > preIndex)
    {
        while(endIndex > preIndex && array[endIndex] >= tmp)
        {
            endIndex--;
        }

        if(preIndex == endIndex)
        {
            break;
        }

        array[preIndex] = array[endIndex];
        preIndex++;

        while(array[preIndex] <= tmp && preIndex < endIndex)
            preIndex++;

        if(preIndex == endIndex)
        {
            break;
        }

        array[endIndex] = array[preIndex];
        endIndex--;


    } // end of while

    array[preIndex]  = tmp;
    qsort(array, begin, preIndex - 1);
    qsort(array, preIndex + 1, end);
    return;
}


int main()
{
    int array[] = {100, 20, 30, 5, 74, 1, 2, 5, 6, 78, 45, 36, 12, 56, 99, 85, 14};
    qsort(array, 0, ARRAYSIZE(array) - 1);

    for(int i = 0; i < ARRAYSIZE(array); i++)
        printf("%d ", array[i]);

    return 0;
}